package com.niit.controller;

import com.niit.pojo.Consumer;
import com.niit.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ConsumerController 控制器类，用于处理与消费者相关的操作
 */
@Controller
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService; // 自动注入 ConsumerService 依赖

    /**
     * 获取所有消费者的端点方法
     * @return 所有消费者的列表
     */
    @GetMapping("/backend-api-endpoint/getAllConsumers")
    @ResponseBody
    public List<Consumer> getAllConsumers() {
        // 调用服务从数据库获取数据
        List<Consumer> consumers = consumerService.getAllConsumers();
        return consumers;
    }

    /**
     * 显示消费者表格页面的方法
     * @return 消费者表格页面的路径
     */
    @RequestMapping("/consumer_table")
    public String consumer_table(){
        return "consumer_table";
    }


    @RequestMapping("/reservation")
    public String reservation(){
        return "reservation";
    }



    @RequestMapping("/order_table_consumer")
    public String order_table_consumer(){
        return "order_table_consumer";
    }


    @RequestMapping("/order_table_window")
    public String order_table_window(){
        return "order_table_window";
    }

    /**
     * 添加消费者的端点方法
     * @param consumer 要添加的消费者对象
     * @return 添加结果信息
     */
    @PostMapping("/backend-api-endpoint/addConsumer")
    @ResponseBody
    public ResponseEntity<String> addConsumer(@RequestBody Consumer consumer) {
        try {
            // 在这里处理新增用户的逻辑，将 consumer 对象保存到数据库
            consumerService.addConsumer(consumer);
            return ResponseEntity.ok("用户添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("添加用户时出错");
        }
    }


    /**
     * 编辑消费者的端点方法
     * @param consumer 要编辑的消费者对象
     * @return 编辑结果信息
     */
    @PostMapping("/backend-api-endpoint/editConsumer")
    @ResponseBody
    public ResponseEntity<String> editConsumer(@RequestBody Consumer consumer) {
        try {
            consumerService.editConsumer(consumer);
            return ResponseEntity.ok("用户编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("编辑用户时出错");
        }
    }


    /**
     * 删除消费者的端点方法
     * @param requestData 删除请求的数据，包含消费者ID
     * @return 删除结果信息
     */
    @PostMapping("/backend-api-endpoint/deleteConsumer")
    @ResponseBody
    public ResponseEntity<String> deleteConsumer(@RequestBody Map<String, String> requestData) {
        String consumerId = requestData.get("consumer_id");

        try {
            consumerService.deleteConsumer(consumerId);
            return ResponseEntity.ok("用户删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除用户时出错");
        }
    }

    @PostMapping("/backend-api-endpoint/updateConsumer")
    public ResponseEntity<String> updateConsumer(@RequestBody Consumer consumer) {
        // 根据 consumer_id 从数据库中获取原有消费者信息
        Consumer existingConsumer = consumerService.getConsumerById(consumer.getConsumer_id());

        // 验证旧密码是否匹配
        if (existingConsumer.getConsumer_password().trim().equals(consumer.getOld_password().trim())) {
            // 更新密码
            existingConsumer.setConsumer_password(consumer.getConsumer_password());
            consumerService.updateConsumer(consumer);
            return new ResponseEntity<>("Consumer information updated successfully.", HttpStatus.OK);
        } else {
            // 旧密码不匹配，返回错误响应
            return new ResponseEntity<>("Old password does not match.", HttpStatus.BAD_REQUEST);
        }
    }




    @GetMapping("/backend-api-endpoint/getConsumerById")
    @ResponseBody
    public ResponseEntity<Consumer> getConsumerById(@RequestParam String consumerId) {
        try {
            Consumer consumer = consumerService.getConsumerById(consumerId);
            System.out.println("Retrieved consumer: " + consumer);
            return ResponseEntity.ok(consumer);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @GetMapping("/edit_consumer_with_params")
    public String editConsumerPage(Model model, @RequestParam String consumerId) {
        // 假设你在 ConsumerService 中有一个通过 ID 获取消费者的方法
        Consumer consumer = consumerService.getConsumerById(consumerId);
        // 将 consumer 对象添加到模型中
        model.addAttribute("consumer", consumer);
        model.addAttribute("consumerId", consumerId);
        // 返回 Thymeleaf 模板名称
        return "edit_consumer";
    }






}
